package pt.utl.ist.project_ccu;

import java.util.ArrayList;
import java.util.TreeMap;

import pt.utl.ist.project_ccu.android_libs.Album;
import pt.utl.ist.project_ccu.android_libs.Photo;
import pt.utl.ist.project_ccu.android_libs.SavedData;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class PhotoToAlbum extends Activity {
	
	Spinner _albumList;
	EditText _albumName;
	AlertDialog _alertDialog;
	Context thisContext;
	Button _addToAlbum;
	Button _createAlbum;
	TextView _createText;
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_to_album);
        thisContext = this;
        SavedData.getInstance().updateActivity(this);
        _albumList = (Spinner) findViewById(R.id.albumList);
        _albumName = (EditText) findViewById(R.id.albumName);
        _addToAlbum = (Button) findViewById(R.id.add_to_album_button);
        _createAlbum = (Button) findViewById(R.id.create_album_button);
        _createText = (TextView) findViewById(R.id.textView2);
        
        TreeMap<String,Album> albumNames = new TreeMap<String,Album>();
        ArrayList<String> names = new ArrayList<String>();
        for (Album al : SavedData.getInstance().getAlbuns()) {
        	albumNames.put(al.getAlbumName(),al);
        	names.add(al.getAlbumName());
        }
        if(names.isEmpty()) {
        	names.add("");
        }
        names.add("Create Album");
        
        ArrayAdapter<String> albumNamesList = new ArrayAdapter<String>(this,R.layout.spinner_item,names);
        albumNamesList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        _albumList.setAdapter(albumNamesList);
        final ArrayList<Photo> photos = (ArrayList<Photo>) getIntent().getSerializableExtra("photos");
        _albumList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
            	String albumName = adapterView.getItemAtPosition(pos).toString();
				if(albumName.equals("Create Album")) {
					setCreateAlbumVisible();
				}
				else {
					setCreateAlbumGone();
				}
            } 

            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            } 
        }); 
        _addToAlbum.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String albumName = _albumList.getSelectedItem().toString();
				if(!albumName.equals("") && !albumName.equals("Create Album")) {
					Album album = new Album(albumName);
					album.addPhotos(photos);
					SavedData.getInstance().getAlbumByName(albumName).addPhotos(photos);
					startActivity(new Intent(getApplicationContext(), Success.class));
				}
				else {
					showDialog(thisContext, "Trying to Add Photos To Album", "No Albuns created");
				}
				
			}
		});
        _createAlbum.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String albumName = _albumName.getText().toString();
				if(albumName != null && !albumName.equals("")) {
					Album album = new Album(albumName);
					album.addPhotos(photos);
					SavedData.getInstance().addAlbum(album);
					startActivity(new Intent(getApplicationContext(), Success.class));
				}
				else {
					showDialog(thisContext, "Failed To Create Album", "No name specified");
				}
				
			}
		});
        
        
    }
    
    private void setCreateAlbumVisible() {
    	_albumName.setVisibility(View.VISIBLE);
    	_createText.setVisibility(View.VISIBLE);
    	_createAlbum.setVisibility(View.VISIBLE);
    	_addToAlbum.setVisibility(View.INVISIBLE);
    }
    
    private void setCreateAlbumGone() {
    	_albumName.setVisibility(View.GONE);
    	_createText.setVisibility(View.GONE);
    	_createAlbum.setVisibility(View.GONE);
    	_addToAlbum.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.photo_to_album, menu);
        return true;
    }
    
	private void showDialog(Context context, String title, String message) {
        if( _alertDialog != null && _alertDialog.isShowing() ) return;

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int arg1) {
                    dialog.dismiss();
                }});
        builder.setCancelable(false);
        _alertDialog = builder.create();
        try {
        	_alertDialog.show();
        }
        catch(Exception e) {}
	}
}
